var username = document.getElementById("username");
var password = document.getElementById("password");
var repeatPassword = document.getElementById("repeatPassword");
var textCode = document.getElementById("textCode");
var xhr;

function displayUser() {
    if(username.value == ""){
        return;
    }
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = processUser;
    xhr.open("GET","isExistAccount?username=" + username.value,true);
    xhr.send(null);
}

function processUser() {
    if(xhr.readyState == 4){
        if(xhr.status == 200){
            var response = xhr.responseText;
            var msg = document.getElementById("isExistInfo");
            if(response == "Exist"){
                msg.setAttribute("class","newAccountFormErrorMsg");
                msg.textContent = "用户名已经存在";
            }
            else{
                //采用classlist的add方法是加新类
                msg.setAttribute("class","newAccountFormOkMsg");
                msg.innerText = "用户名可以使用";
            }
        }
    }
}

function displayPass() {
    if(password.value == ""){
        return;
    }
    var str = password.value;
    if(str.length < 8 || str.length > 16){
        var msg = document.getElementById("isRightPass");
        msg.setAttribute("class","newAccountFormErrorMsg");
        msg.innerText = "密码长度应该大于8并不超过16";
    }
    else{
        var msg = document.getElementById("isRightPass");
        msg.setAttribute("class","newAccountFormOkMsg");
        msg.innerText = "密码可以使用";
    }
}

function displayRepPass(){

    if(repeatPassword.value == ""){
        return;
    }

    if(repeatPassword.value != password.value){
        var msg = document.getElementById("isRepeatPass");
        msg.setAttribute("class","newAccountFormErrorMsg");
        msg.innerText = "请保证密码输入一致";
    }
    else{
        var msg = document.getElementById("isRepeatPass");
        msg.setAttribute("class","");
    }
}

function displayTextCode() {
    if(textCode.value == ""){
        return;
    }
    xhr = new XMLHttpRequest();
    xhr.onreadystatechange = processTextCode;
    xhr.open("GET","isRightTextCode?textCode=" + textCode.value,true);
    xhr.send(null);
}

function processTextCode() {
    if(xhr.readyState == 4){
        if(xhr.status == 200){
            var response = xhr.responseText;
            var msg = document.getElementById("isRightTextCode");
            if(response == "Right"){
                msg.setAttribute("class","signOnOkMsg");
                msg.textContent = "验证码正确";
            }
            else{
                msg.setAttribute("class","signOnErrorMsg");
                msg.textContent = "验证码输入错误";
            }
        }
    }
}

function change(){
    var msg = document.getElementById("isExistInfo");
    msg.textContent = "";
}

function changePass(){
    var msg = document.getElementById("isRightPass");
    msg.textContent = "";
}

function changeRepPass(){
    var msg = document.getElementById("isRepeatPass");
    msg.textContent = "";
}

function changeTextCode(){
    var msg = document.getElementById("isRightTextCode");
    msg.textContent = "";
}

username.onblur = displayUser;
username.oninput = change;
password.onblur = displayPass;
password.oninput = changePass;
repeatPassword.onblur = displayRepPass;
repeatPassword.oninput = changeRepPass;
textCode.onblur = displayTextCode;
textCode.oninput = changeTextCode;

$(document).ready(function () {
    $("#newAccountForm").submit(function () {
        var msgUsr = document.getElementById("isExistInfo");
        var msgPass = document.getElementById("isRightPass");
        var msgRepPass = document.getElementById("isRepeatPass");
        var msgTextCode = document.getElementById("isRightTextCode");

        if(msgUsr.className == "newAccountFormErrorMsg"){
            alert("用户名已经存在");
            username.focus();
            return false;
        }
        else if(msgPass.className == "newAccountFormErrorMsg"){
            alert("密码格式错误");
            password.focus();
            return false;
        }
        else if(msgRepPass.className == "newAccountFormErrorMsg"){
            alert("两次密码不一致!");
            repeatPassword.focus();
            return false;
        }
        else if(msgTextCode.className == "signOnErrorMsg"){
            alert("验证码输入错误");
            textCode.focus();
            return false;
        }
        return true;

    })
})
